package users

import (
	"fmt"
	"ha666web/controllers"
	"ha666web/models"
	"time"
)

type UserLoginInfoController struct {
	controllers.BaseController
}

func (this *UserLoginInfoController) Get() {
	this.TplName = "users/logininfo.tpl"
}

func (this *UserLoginInfoController) Post() {
	defer func() {
		if err := recover(); err != nil {
			this.Data["json"] = map[string]interface{}{"code": 0, "message": fmt.Sprintf("异常:%v", err)}
			this.ServeJSON()
			return
		}
	}()
	time.Sleep(2 * time.Second)

	//判断page_index参数
	page_index, err := this.GetInt("page_index", -1)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "查询page_index出错:" + err.Error()}
		this.ServeJSON()
		return
	}
	if page_index < 1 || page_index > 1000 {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "page_index参数错误"}
		this.ServeJSON()
		return
	}

	//判断page_size参数
	page_size, err := this.GetInt("page_size", -1)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "查询page_size出错:" + err.Error()}
		this.ServeJSON()
		return
	}
	if page_size < 1 || page_size > 100 {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "page_size参数错误"}
		this.ServeJSON()
		return
	}

	//查询user_id参数
	user_id := this.UserId
	if user_id <= 0 {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "user_id参数错误"}
		this.ServeJSON()
		return
	}

	//查询登录记录列表
	logininfos, err := models.GetLoginInfoByUserIdLoginTime(user_id, page_index, page_size)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "查询登录记录列表出错:" + err.Error()}
		this.ServeJSON()
		return
	}

	//返回结果
	this.Data["json"] = map[string]interface{}{"code": 1, "message": logininfos}
	this.ServeJSON()
}
